﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Sunyi.Project.WebPoint.BusinessLogic;
using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataContract;
using Sunyi.Project.WebPoint.WebControl;

using ActivitySystem.BusinessLogic;
using ActivitySystem.Common;
using ActivitySystem.DataContract;


namespace ActivitySystem.Web.Activity
{
    public partial class Host : WebPointPage
    {
        private ActivityTypeManager activityTypeManager = new ActivityTypeManager();
        private ActivityManager activityManager = new ActivityManager();
        private UserManager userManager = new UserManager();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    BindActivityType();

                    if (!string.IsNullOrEmpty(Request.Params[ActivitySystemGlobalConstant.ActivityId]))
                    {
                        Guid activityId = new Guid(Request.Params[ActivitySystemGlobalConstant.ActivityId]);
                        BindActivity(activityId);
                    }
                    else
                    {
                        SetControlValue();
                    }
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(this, ex);
                }
            }
        }

        private void SetControlValue()
        {
            tbxStartTime.Text = DateTime.Now.ToString(ActivitySystemGlobalConstant.TimeEditFormatString);
            tbxEndTime.Text = DateTime.Now.AddHours(4).ToString(ActivitySystemGlobalConstant.TimeEditFormatString);
        }

        private void BindActivity(Guid activityId)
        {

        }

        private void BindActivityType()
        {
            var activityTypes = activityTypeManager.SelectAll().Select(p => new { p.Id, p.Name });
            rptAvailableActivityTypes.DataSource = activityTypes;
            rptAvailableActivityTypes.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbxTitle.Text))
            {
                ShowClientMessage("Title should not be empty");
                return;
            }

            if (string.IsNullOrWhiteSpace(tbxActivityType.Text))
            {
                ShowClientMessage("Type cannot be empty");
            }

            try
            {
                var activity = new ActivitySystem.DataContract.Activity();
                activity.Title = tbxTitle.Text;
                activity.Description = tbxDescription.Text;
                activity.StartTime = DateTime.Parse(tbxStartTime.Text);
                activity.EndTime = DateTime.Parse(tbxEndTime.Text);
                activity.IsPublic = chkIsPublic.Checked;

                Collection<ActivityType> types = new Collection<ActivityType>();
                foreach (string name in tbxActivityType.Text.Split(WebPointGlobalConstant.Comma.ToCharArray()))
                {
                    ActivityType type = activityTypeManager.SelectByName(name.Trim());
                    if (type != null)
                    {
                        types.Add(type);
                    }
                    else
                    {
                        ShowClientMessage(string.Format("Activity type '{0}' does not exist.", name));
                        return;
                    }
                }

                Collection<User> invitedUsers = new Collection<User>();
                foreach (string name in tbxInvite.Text.Split(WebPointGlobalConstant.Comma.ToCharArray()))
                {
                    User user = userManager.SelectByName(name.Trim());
                    if (user != null)
                    {
                        invitedUsers.Add(user);
                    }
                    else
                    {
                        ShowClientMessage(string.Format("User '{0}' does not exist.", name));
                        return;
                    }
                }

                activityManager.Host(activity, CurrentUser, types, invitedUsers);

                ClientRedirect("List.aspx");
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }
    }
}